<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div>
    <input id="text" type="text" value="Runoob" />
    <button id="sendMessage">发送消息</button>
  </div>
  <iframe loading="lazy" id="receiver" src="./postMessage_receiver.html" width="300" height="360">
    <p>你的浏览器不支持 iframe。</p>
  </iframe>
  <script>
    window.onload = function () {
      var receiver = document.getElementById('receiver').contentWindow;
      var btn = document.getElementById('sendMessage');
      btn.addEventListener('click', function (e) {
        e.preventDefault();
        var val = document.getElementById('text').value;
        receiver.postMessage("Hello " + val + "！", "*");
        window.open('./postMessage_receiver.html')
      });
    }
  </script>
</body>

</html>
